﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
 
   public static class IOManager
    {

        /// <summary>
        /// 写入字节
        /// </summary>
        /// <param name="path"></param>
        /// <param name="msg"></param>
        /// <param name="Delete"></param>
        public static void SaveFile(string path, string msg, bool Delete)
        {
            CheckPath(path);
            if (Delete)
            {
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
            }
            File.WriteAllText(path, msg);
        }

        public static void SaveFile(string path, byte[] msg, bool Delete)
        {
            CheckPath(path);
            if (Delete)
            {
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
            }
            File.WriteAllBytes(path, msg);
        }

        /// <summary>
        /// Load
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string LoadFile(string path)
        {
            if (File.Exists(path))
            {

                return File.ReadAllText(path);
            }
            return "";
        }

        public static byte[] LoadFile(string path, bool b)
        {
            if (File.Exists(path))
            {

                return File.ReadAllBytes(path);
            }
            return new byte[0];
        }

        public static void DeleteFile(string path)
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }
        }

        /// <summary>
        /// Exit
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool ExitFile( string path)
        {
            return File.Exists(path);
        }

        /// <summary>
        /// Create Folder
        /// </summary>
        /// <param name="path"></param>
        private static void CheckPath(string path)
        {
            string m_pt = path;
            var sp = m_pt.Split('/');
            var len = m_pt.Replace(sp[sp.Length - 1], "");
            if (Directory.Exists(len) == false)
            {
                Directory.CreateDirectory(len);
            }
        }

    }
